package org.pentaho.cws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.pentaho.cws.action.types.ActionDescription;
import org.pentaho.cws.action.types.ActionInfo;
import org.pentaho.platform.api.action.IAction;
import org.pentaho.platform.api.engine.IPluginManager;
import org.pentaho.platform.api.engine.PluginBeanException;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.util.beans.ActionHarness;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;

@Path("/cws/api/actions")
public class ActionsResource {
  
  protected IPluginManager pluginMgr = PentahoSystem.get(IPluginManager.class);

//  @GET
//  public List<ActionInfo> getActions() {
//    ArrayList<ActionInfo> actionInfos = new ArrayList<ActionInfo>();
//    for(String pluginId : pluginMgr.getRegisteredPlugins()) {
//      ListableBeanFactory pluginBeanFactory = pluginMgr.getBeanFactory(pluginId);
//      Map<String, IAction> actions = BeanFactoryUtils.beansOfTypeIncludingAncestors(pluginBeanFactory, IAction.class);
//      for(Map.Entry<String, IAction> entry : actions.entrySet()) {
//        ActionInfo actionInfo = new ActionInfo();
//        populate(entry.getValue(), entry.getKey(), actionInfo, pluginId);
//        actionInfos.add(actionInfo);
//      }
//    }
//    return actionInfos;
//  }
  
  protected void populate(IAction action, String actionName, ActionInfo info, String pluginId) {
    info.setClassName(action.getClass().getName());
    info.setPlugin(pluginId);
    info.setName(actionName);
  }
  
//  @GET
//  @Path("{name}/desc")
//  @Consumes("text/plain")
//  public ActionDescription getActionDescription(@PathParam("name") String name) {
//    IAction action = null;
//    try {
//      action = (IAction)pluginMgr.getBean(name);
//    } catch (PluginBeanException e) {
//      // TODO Auto-generated catch block
//      e.printStackTrace();
//    }
//    ActionHarness harness = new ActionHarness(action);
//    Map<String, String> descMap = harness.describe();
//    System.out.println(descMap);
//    ActionDescription desc = new ActionDescription();
//    desc.setClassName(action.getClass().getName());
//    desc.setName(name);
//    desc.setProperties(descMap.keySet());
//    return desc;
//  }
}
